package org.example.ui;

import org.example.dao.EmployeeDao;
import org.example.dao.impl.EmployeeDaoImpl;
import org.example.entity.Employee;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

public class EmployeeManagementPanel2 extends JPanel {
    private JTable table;
    private DefaultTableModel tableModel;
    private JTextField nameField;
    private JButton deleteButton;
    private EmployeeDao employeeDao = new EmployeeDaoImpl();

    public EmployeeManagementPanel2() {
        setLayout(new BorderLayout());

        // 北部面板，放置输入框和删除按钮
        JPanel northPanel = new JPanel();
        JLabel label = new JLabel("输入要删除的员工姓名:");
        nameField = new JTextField(15);
        label.setFont(new Font("行书", Font.BOLD, 25));
        deleteButton = new JButton("删除");
        northPanel.add(label);
        northPanel.add(nameField);
        northPanel.add(deleteButton);
        add(northPanel, BorderLayout.NORTH);

        // 中间面板，放置表格
        String[] columnNames = {"姓名", "职位", "雇佣日期", "薪资", "佣金", "部门ID"};
        tableModel = new DefaultTableModel(columnNames, 0);
        table = new JTable(tableModel);
        table.setShowHorizontalLines(true);
        table.setShowVerticalLines(true);
        table.setGridColor(new Color(220, 220, 220));
        JScrollPane scrollPane = new JScrollPane(table);
        add(scrollPane, BorderLayout.CENTER);

        // 加载数据库中的员工信息
        loadEmployees();

        // 删除按钮点击事件处理
        deleteButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String empName = nameField.getText();
                if (!empName.isEmpty()) {
                    // 根据姓名查找员工ID
                    List<Employee> employees = employeeDao.getAllEmployees();
                    for (Employee employee : employees) {
                        if (employee.getEmpName().equals(empName)) {
                            int empId = employee.getEmpId();
                            // 调用删除方法
                            employeeDao.deleteEmp(empId);
                            JOptionPane.showMessageDialog(EmployeeManagementPanel2.this, "员工删除成功！");
                            // 重新加载数据
                            loadEmployees();
                            nameField.setText("");
                            return;
                        }
                    }
                    JOptionPane.showMessageDialog(EmployeeManagementPanel2.this, "未找到该员工！");
                } else {
                    JOptionPane.showMessageDialog(EmployeeManagementPanel2.this, "请输入要删除的员工姓名！");
                }
            }
        });
    }

    private void loadEmployees() {
        tableModel.setRowCount(0);
        List<Employee> employees = employeeDao.getAllEmployees();
        for (Employee employee : employees) {
            tableModel.addRow(new Object[]{
                    employee.getEmpName(),
                    employee.getJob(),
                    employee.getHireDate(),
                    employee.getSal(),
                    employee.getComm(),
                    employee.getDept_id()
            });
        }
    }
}